#ifndef APP_H
#define APP_H

#include <stdint.h>
#include "pid.h"
#include "gpio.h"

#define DEBUG 0

//ADC相关
#define ADC_MAXVALU                                 4096                                //ADC最大量程
#define ADC_VREF                                    3.300f                              //Vref
#define ADC_FACT                                    ADC_VREF / ADC_MAXVALU

#define V_BIAS                                      0                                   //电压采样偏置
#define V_GAIN_2350                                 20.05f                               //OPA2350电压增益

#define V_RATIO_5050                                ADC_FACT * V_GAIN_2350

#define I_BIAS                                      2048                                //电流采样偏置
#define I_GAIN_A1                                   1.0f / 20.0f                        //INA240A1电压增益20倍
#define I_GAIN_A2                                   1.0f / 50.0f                        //INA240A2电压增益50倍
#define R_SAMP_INV_2M                               500.0f                              //2mOhm采样电阻倒数
#define R_SAMP_INV_4M                               250.0f                              //4mOhm采样电阻倒数
#define R_SAMP_INV_10M                              100.0f                              //10mOhm采样电阻倒数

#define I_RATIO_A1_2M                               ADC_FACT * I_GAIN_A1 * R_SAMP_INV_2M
#define I_RATIO_A1_4M                               ADC_FACT * I_GAIN_A1 * R_SAMP_INV_4M
#define I_RATIO_A1_10M                              ADC_FACT * I_GAIN_A1 * R_SAMP_INV_10M
#define I_RATIO_A2_2M                               ADC_FACT * I_GAIN_A2 * R_SAMP_INV_2M
#define I_RATIO_A2_4M                               ADC_FACT * I_GAIN_A2 * R_SAMP_INV_4M
#define I_RATIO_A2_10M                              ADC_FACT * I_GAIN_A2 * R_SAMP_INV_10M

#define R_UP                                        680.0f
#define R_DOWN                                      100.0f
#define V_RATIO                                     ADC_FACT*((R_UP+R_DOWN)/R_DOWN)

// 设置短按和长按时间（以ms为单位）
#define SHORT_PRESS_THRESHOLD 50    // 短按时间阈值
#define LONG_PRESS_THRESHOLD 3000   // 长按时间阈值
#define TRIPLE_PRESS_THRESHOLD 500 // 连击时间阈值

#define SHUT_DOWN_THRESHOLD 5500           // 关机延时
#define CAP_VLOTAGE_DROP_THRESHOLD 14.0f    // 触发关机阈值
#define CAP_VLOTAGE_RAISE_THRESHOLD 12.5f    // 允许开机阈值

#define CAP_VLOTAGE_CHARGE_VARIBILITY 0.0003f        // 电压变异率
#define CAP_VLOTAGE_DISCHARGE_VARIBILITY 0.00002f    // 电压变异率

#define CAP_VLOTAGE_THRESHOLD 5.0f          // 电容放电截止，避免调试锁死
#define BAT_VLOTAGE_THRESHOLD 7.4f          // 电池放电截止，触发关机报文

#define BOOT_STARTUP_DELAY_TIME 1500 // 开机状态稳定时间
#define BOOT_SHUTDOWN_DELAY_TIME 1500   // 关机状态稳定时间

enum CAP_CHARGE_STATE
{
    CAP_STABLE = 0,
    CAP_CHARGE = 1,
    CAP_DISCHARGE = 2,
};


enum SYS_RUN_STATE
{
    SYS_OFF = 0,
    SYS_ON = 1,
};
/*
 * IF 电容电压>CAP_VLOTAGE_RAISE_THRESHOLD and 电容电压上升
 * THEN 开机
 *
 * IF 电容电压<CAP_VLOTAGE_DROP_THRESHOLD
 * THEN 关机
 */

typedef struct
{
    uint8_t sw_state;
    enum CAP_CHARGE_STATE sys_state;
    enum CAP_CHARGE_STATE sys_state_next;
    enum CAP_CHARGE_STATE sys_state_next_last;
    uint8_t ip_state;
    uint8_t pc_state;
    uint8_t pd_state;
    uint32_t ip_delay;
    uint32_t pc_delay;

    enum CAP_CHARGE_STATE charge_state;
    enum CAP_CHARGE_STATE charge_state_last;

    float cap_current;
    float cap_current_target;
    float cap_vlotage;
    float cap_vlotage_last;
    unsigned int adc1_buf[1];
    unsigned int adc2_buf[1];
    float lpf;

    pid_type_def current_loop;
    pid_type_def out_loop;
    float ratio;

} system_state_t;

/**
 * @brief APP初始化,请在开启rtos之前调用.这也是唯一需要放入main函数的函数
 *
 */
void APPInit(void);

/**
 * @brief APP任务,放入实时系统以一定频率运行,内部会调用各个应用的任务
 *
 */
void APPTask(void);
void TIM17_Handle(void);

#endif // !APP_H
